package com.nuoniu.sibanyun.service.erp.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.entity.dto.ErpVoucherTypeDto;
import com.nuoniu.sibanyun.entity.erp.ErpVoucherType;
import com.nuoniu.sibanyun.entity.vo.ErpVoucherTypeVo;
import com.nuoniu.sibanyun.mapper.erp.ErpVoucherTypeMapper;
import com.nuoniu.sibanyun.service.erp.IErpVoucherTypeService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 单据编码规则 服务实现类
 * </p>
 *
 * @author baiqiang
 * @since 2020-09-28
 */
@Service
public class ErpVoucherTypeServiceImpl extends ServiceImpl<ErpVoucherTypeMapper, ErpVoucherType> implements IErpVoucherTypeService {

    @Override
    public void initDefData(Integer companyId) {
        QueryWrapper<ErpVoucherType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",companyId);
        List<ErpVoucherType> erpVoucherTypeVos = list(queryWrapper);
        if(erpVoucherTypeVos.size() > 0 ) {
            return;
        }
        //获取 默认单据配置
        List<ErpVoucherType> erpVoucherTypes = listDefErpVoucherType();
        erpVoucherTypes.forEach(f->{
            f.setId(null);
            f.setCompanyId(companyId);
        });
        if(erpVoucherTypes.size() > 0){
            saveBatch(erpVoucherTypes);
        }
    }

    @Override
    public List<ErpVoucherType> listDefErpVoucherType() {
        QueryWrapper<ErpVoucherType>  queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",1);
        List<ErpVoucherType> list = list(queryWrapper);
        return list;
    }

    @Override
    public List<ErpVoucherTypeVo> listCurrentErpVoucherTypeVo() {
        UserInfo userInfo = UserThreadLocal.get();
        List<ErpVoucherTypeVo> erpVoucherTypeVos = baseMapper.listCurrentErpVoucherTypeVo(userInfo.getCompanyId());
        return erpVoucherTypeVos;
    }

    @Override
    public void updateOne(ErpVoucherTypeDto erpVoucherTypeDto) {
        Long id = erpVoucherTypeDto.getId();
        if(null == id) {
            throw new NuoNiuException("参数错误,ID不能为空");
        }
        ErpVoucherType oldOne = getById(id);
        if(null == oldOne) {
            throw new NuoNiuException("参数错误,ID不正确");
        }
        ErpVoucherType erpVoucherType = new ErpVoucherType();
        BeanUtils.copyProperties(erpVoucherTypeDto,erpVoucherType);
        updateById(erpVoucherType);
    }

    @Override
    public ErpVoucherType getById(Integer id) {
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper<ErpVoucherType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",userInfo.getCompanyId());
        queryWrapper.last("LIMIT 1");
        ErpVoucherType one = getOne(queryWrapper, false);
        return one;
    }
}
